//
//  TopScreenView.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-19.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "TopScreenView.h"
#import "SVGloble.h"
#import "RootScreenView.h"


//按钮空隙
#define BUTTONGAP 0
//滑条宽度
#define CONTENTSIZEX (400)
//按钮id
#define BUTTONID (sender.tag-100)
//滑动id
#define BUTTONSELECTEDID (scrollViewSelectedChannelID - 100)

//extern  BOOL    isNodePagePlay;//是否是在节点界面播放。是：YES.否：NO.

extern  NSInteger   lastFlowNumber;//前一个处理的流程No.或者当前

@implementation TopScreenView
@synthesize nameArray;
@synthesize scrollViewSelectedChannelID;



//+ (TopScreenView *)shareInstance {
//    static TopScreenView *_instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instance=[[self alloc] initWithFrame:CGRectMake(0, 0, 400, 40)];
//    });
//    return _instance;
//}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.userInteractionEnabled = YES;
        
        self.pagingEnabled = NO;
        self.bounces = NO;
        
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        self.backgroundColor = [UIColor clearColor];
        
        userSelectedChannelID = 100;
        scrollViewSelectedChannelID = 100;
        
        self.buttonOriginXArray = [NSMutableArray array];
        self.buttonWithArray = [NSMutableArray array];
        
    }
    return self;
}



- (void)initWithNameButtons
{
    float xPos = 0.0;
    for (int i = 0; i < [self.nameArray count]; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *title = [self.nameArray objectAtIndex:i];
        if(![Common isEmptyString:title]){
        NSInteger index = [title rangeOfString:@"【"].location;
        title = [title substringWithRange:NSMakeRange(0, index)];
        }
        else{
            title = NullString;
        }
        [button setTag:i+100];
        if (i == [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"screenNo"] intValue]) {
            button.selected = YES;
            [button setBackgroundImage:[UIImage imageNamed:@"screenNotSelected.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"screenSelected.png"] forState:UIControlStateSelected];
        }
        else
        {
            [button setBackgroundImage:[UIImage imageNamed:@"screenNotSelected.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"screenSelected.png"] forState:UIControlStateSelected];
        }
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
        
        //        int buttonWidth = [title sizeWithFont:button.titleLabel.font
        //                            constrainedToSize:CGSizeMake(430, 200)
        //                                lineBreakMode:NSLineBreakByClipping].width;
        
        button.frame = CGRectMake(xPos, 0, 100, 40);
        //[button setBackgroundColor:[UIColor grayColor]];
        [button setBackgroundColor:[UIColor clearColor]];
        
        [_buttonOriginXArray addObject:@(xPos)];
        
        xPos += 100+BUTTONGAP;
        
        [_buttonWithArray addObject:@(button.frame.size.width)];
        
        [self addSubview:button];
    }
    
    self.contentSize = CGSizeMake(xPos, 40);
    
    //shadowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(BUTTONGAP, 0, [[_buttonWithArray objectAtIndex:0] floatValue], 200)];
    //[shadowImageView setImage:[UIImage imageNamed:@"red_line_and_shadow.png"]];
    //[self addSubview:shadowImageView];
}

//点击顶部条滚动标签
- (void)selectNameButton:(UIButton *)sender
{
    //如果更换按钮
    if (sender.tag != [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"screenNo"] intValue]+100) {
        //取之前的按钮
        UIButton *lastButton = (UIButton *)[self viewWithTag:[allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"screenNo"] intValue]+100];
        lastButton.selected = NO;
        //赋值按钮ID
        allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"screenNo"] = @(sender.tag-100);
        userSelectedChannelID = sender.tag;
    }
    //按钮选中状态
    if (!sender.selected) {
        sender.selected = YES;
        [UIView animateWithDuration:0.25 animations:^{
        } completion:^(BOOL finished) {
            if (finished) {
                NSLog(@"%ld",BUTTONID);
                //设置新闻页出现
                if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1)
                {
                    [self.delagateTopScreenForTap HandleTapScreenButton:self ScreenIndex:@(BUTTONID)];
                }
                else
                {
                    [self.delagateTopScreenForTap HandleTapScreenButtonForFlowControl:self ScreenIndex:@(BUTTONID)];
                }
                //赋值滑动列表选择频道ID
                scrollViewSelectedChannelID = sender.tag;
            }
        }];
        
    }
    //重复点击选中按钮
    else {
        
    }
}


//滚动内容页顶部滚动
- (void)setButtonUnSelect
{
    //滑动撤销选中按钮
    UIButton *lastButton = (UIButton *)[self viewWithTag:scrollViewSelectedChannelID];
    lastButton.selected = NO;
}

- (void)setButtonSelect
{
    //滑动选中按钮
    UIButton *button = (UIButton *)[self viewWithTag:scrollViewSelectedChannelID];
    
    [UIView animateWithDuration:0.25 animations:^{
        
        //[shadowImageView setFrame:CGRectMake(button.frame.origin.x, 0, [[_buttonWithArray objectAtIndex:button.tag-100] floatValue], 40)];
        
    } completion:^(BOOL finished) {
        if (finished) {
            if (!button.selected) {
                button.selected = YES;
                allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"screenNo"] = @(button.tag-100);
                userSelectedChannelID = button.tag;
            }
        }
    }];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
